package com.W04;

import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class Calculator extends AppCompatActivity {
    Spinner spinner;
    EditText ed_num1,ed_num2;
    Button button;
    TextView tv_show;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculator);
        init();
        String[] ops={"+","-","*","/"};
        spinner.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item,
                ops
        ));
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int pos=spinner.getSelectedItemPosition();
                int num1=Integer.parseInt(ed_num1.getText().toString());
                int num2=Integer.parseInt(ed_num2.getText().toString());
                int result=0;
                switch (pos){
                    case 0:
                        result=num1+num2;
                        break;
                    case 1:
                        result=num1-num2;
                        break;
                    case 2:
                        result=num1*num2;
                        break;
                    case 3:
                        if (num2==0){
                            tv_show.setText("除数不能为0");
                            return ;
                        }else{
                            result=num1/num2;
                        }
                        break;
                }
                tv_show.setText(""+result);
            }
        });

    }

    public void init() {
        //spinner=findViewById(R.id.spinner);

    }



}
